<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<body>
<form id="treeForm"  role="form" method="post" action="tree/save">
    <input type="hidden" name="pId" th:value="${entity.id}" />
    <input type="hidden" name="type" value="1" />
    <div class="row">
        <div class="col-xs-6" >
            <label >父菜单名称:</label>
            <input type="text" class="form-control" th:value="${entity.name}" readonly="readonly"  />
        </div>
        <div class="col-xs-6">
            <label for="name">菜单名称:</label>
            <input type="text" class="form-control" name="name" id="name"  placeholder="请输入菜单名称" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <label for="url">菜单地址:</label>
            <input type="text" class="form-control" name="url" id="url"  placeholder="请输入菜单地址" />
        </div>
        <div class="col-xs-6">
            <label for="icon">菜单样式:</label>
            <input type="text" class="form-control" name="icon" id="icon"  placeholder="请输入菜单样式" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <label for="url">菜单编码:</label>
            <input type="text" class="form-control" name="code" id="code"  placeholder="请输入菜单编码" />
        </div>
        <div class="col-xs-6">
            <label for="treeOrder">菜单顺序:</label>
            <input type="text" class="form-control" name="treeOrder" id="treeOrder"  placeholder="请输入菜单顺序" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-6">
            <label >菜单状态:</label>
            <div class="col-xs-12">
                <label  class='radio-inline'><input style="vertical-align: middle;margin-top:2px; margin-bottom:1px;" type='radio' name='state' value='1'  checked="checked"  />可用</label>
                <label  class='radio-inline'><input style="vertical-align: middle;margin-top:2px; margin-bottom:1px;" type='radio' name='state' value='0'   />禁用</label>
            </div>
        </div>
    </div>
</form>
<script th:inline="javascript">
    <![CDATA[
    $(function () {

        $('#treeForm',window.top.document).bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    message: '菜单名称验证失败',
                    validators: {
                        notEmpty: {
                            message: '菜单名称不能为空'
                        }
                    }
                },
                url: {
                    message: '菜单地址验证失败',
                    validators: {
                        notEmpty: {
                            message: '菜单地址不能为空'
                        }
                    }
                },
                code: {
                    message: '菜单编码验证失败',
                    validators: {
                        notEmpty: {
                            message: '菜单编码不能为空'
                        }
                    }
                },
                treeOrder: {
                    message: '菜单顺序验证失败',
                    validators: {
                        notEmpty: {
                            message: '菜单顺序不能为空'
                        },
                        regexp: {
                            regexp: /^[0-9_]+$/,
                            message: '菜单顺序必须为数字'
                        },
                        stringLength: {
                            min: 1,
                            max: 18,
                            message: '菜单顺序必须在1到18位之间'
                        }
                    }
                }
            }
        })

        // 绑定dialog的确定按钮的监听事件
        $("#btnOk",window.top.document).click(function() {
            $("#btnOk",window.top.document).attr("disabled",true);
            var bootstrapValidator = $("#treeForm", window.top.document).data('bootstrapValidator');
            bootstrapValidator.validate();
            if(bootstrapValidator.isValid()){
                var zTree = getCurrentIframe().contentWindow.zTree;
                var selectNode = getCurrentIframe().contentWindow.selectNode;
                $.post($("#treeForm",window.top.document).attr('action'),$("#treeForm",window.top.document).serialize(),function(e){
                    if(e.result){
                        $('.modal-dialog', window.top.document).parent('div').remove()
                        $('body', window.top.document).find('.modal-backdrop').remove();
                        var entity = e.entity;
                        zTree.addNodes(selectNode, {id:entity.id, pId:entity.pId, name:entity.name});
                        window.Ewin.alert({message:'增加数据成功!'});
                    }else{
                        window.Ewin.alert({message:'增加数据失败!'});
                    }
                    $("#btnOk",window.top.document).attr("disabled",false);
                })
            }else{
                $("#btnOk",window.top.document).attr("disabled",true);
            }
        });

        // 获取当前的所在的iframe页面
        function getCurrentIframe(){
            var iframes = $(window.parent.document).contents().find(".J_iframe");
            for(var i=0;i<iframes.length;i++){
                if($(iframes[i]).css("display")=='inline'){
                    return iframes[i];
                }
            }
            return undefined;
        }

    })
    ]]>
</script>
</body>
</html>